<?php
/**
 * Token Service
 * User: zng
 * Date: 2019/11/4
 * Time: 15:07
 */
namespace app\admin\service;

use think\facade\Request;
use Firebase\JWT\{
    JWT,SignatureInvalidException
};

Class Token {

    public static function encode( array $userInfo=[] )
    {
        $key = config('jwt.key');
        $domain = Request::domain();
        $time = time();
        $token = [
            'iss' => $domain,
            'aud' => config('app.app_host'),
            'iat' => $time,
            'nbf' => $time,
            'userInfo' => $userInfo
        ];
        return [
            'token' => JWT::encode($token, $key, 'HS256')
        ];
    }

    public static function decode( string $token )
    {
        $key = config('jwt.key');
        try{
            return returnData((array)JWT::decode($token, $key, ['HS256']));
        }catch (SignatureInvalidException $e) {
            return returnData([], '签名验证失败','422');
        }
    }

}